module "vpc" {
  source     = "../module/vpc"
  secret_id  = var.secret_id
  secret_key = var.secret_key
}

module "cvm" {
  source        = "../module/cvm"
  secret_id     = var.secret_id
  secret_key    = var.secret_key
  password      = var.password
  vpc_id        = module.vpc.vpc_id
  subnet_id     = module.vpc.subnet_id
  instance_name = "cluster-1"
  cpu           = 4
  memory        = 8
}

module "k3s" {
  source      = "../module/k3s"
  public_ip   = module.cvm.public_ip
  private_ip  = module.cvm.private_ip
  server_name = "k3s-hongkong-1"
  pods_cidr = "10.1.0.0/16"
  service_cidr = "10.11.0.0/16"
}

resource "local_sensitive_file" "kubeconfig" {
  content  = module.k3s.kube_config
  filename = "${path.module}/k3s-config1.yaml"
}

module "helm" {
  source           = "../module/helm"
  filename         = "${path.module}/k3s-config1.yaml"
  kubernetes_ready = module.k3s.kubernetes_ready
  helm_charts = [
    {
      name             = "argocd"
      namespace        = "argocd"
      repository       = "https://argoproj.github.io/argo-helm"
      chart            = "argo-cd"
      values_file      = "./argocd/values.yaml"
      create_namespace = true
      timeout = 10
      version = "5.51.6"
      set = [
        {
          "name" : "",
          "value" : "",
        }
      ]
    },
    {
      name             = "loki"
      namespace        = "monitoring"
      repository       = "https://grafana.github.io/helm-charts"
      chart            = "loki-stack"
      values_file      = ""
      create_namespace = true
      timeout = 10
      version = ""
      set = [
        {
          "name" : "loki.isDefault",
          "value" : "false",
        }
      ]
    },
    {
      name             = "kube-prometheus-stack"
      namespace        = "monitoring"
      repository       = "https://prometheus-community.github.io/helm-charts"
      chart            = "kube-prometheus-stack"
      values_file      = "./prometheus/values.yaml"
      create_namespace = true
      timeout = 10
      version = ""
      set = [
        {
          "name" : "",
          "value" : "",
        }
      ]
    },
  ]
}

resource "null_resource" "connect_ubuntu" {
  depends_on = [module.k3s, module.helm]
  connection {
    host     = module.cvm.public_ip
    type     = "ssh"
    user     = "ubuntu"
    password = var.password
  }

  triggers = {
#    id = "${uuid()}"
    script_hash = filemd5("./jenkins/init.sh")
  }

  provisioner "file" {
    source      = "${path.module}/k3s-config1.yaml"
    destination = "/tmp/k3s-config1.yaml"
  }

  provisioner "file" {
    source      = "./jenkins/init.sh"
    destination = "/tmp/init.sh"
  }

  provisioner "file" {
    source      = "./jenkins/values.yaml"
    destination = "/tmp/jenkins-values.yaml"
  }

  provisioner "remote-exec" {
    # script = "/tmp/init.sh"
    inline = [<<EOF
      export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
      kubectl wait --for=condition=Ready pod --all -n kube-system --timeout=900s

      chmod +x /tmp/init.sh
      sh /tmp/init.sh

      export PATH=$PATH:/home/ubuntu/.local/bin

      echo 'public_ip: ${module.cvm.public_ip}'
      echo '${module.cvm.public_ip} argocd.test.chaosx.cn' | sudo tee -a /etc/hosts

      if [ ! -e /usr/local/bin/argocd ]; then
        curl -sSL -o argocd-linux-amd64 https://github.com/argoproj/argo-cd/releases/latest/download/argocd-linux-amd64
        sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
      fi

      if [ ! -e /usr/local/bin/karmadactl ]; then
        git clone https://github.com/karmada-io/karmada
        karmada/hack/install-cli.sh
      fi

      curl -Ls https://get.submariner.io | bash

      kubectl patch  svc argocd-server -n argocd --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"},{"op":"add","path":"/spec/ports/0/nodePort","value":30090}]'
      kubectl patch  svc jenkins -n jenkins --type='json' -p '[{"op":"replace","path":"/spec/type","value":"NodePort"},{"op":"add","path":"/spec/ports/0/nodePort","value":30091}]'

      EOF
      #"kubectl wait --for=condition=Ready pod --all -n kube-system --timeout=900s",
      #"echo ${public_ip} sonar.${domain}" | sudo tee -a /etc/hosts"
    ]
  }

}

